<?php
class  main extends top
{
	function index(){
		$data = $this->spArgs();
		$temple_id = is_numeric($data['temple_id'])?$data['temple_id']:0;
		
		$temple = spClass("m_temple");
		if(isset($data['temple_new'])&&isset($data['title'])){
			$temple_id = $temple->create(array('title'=>$data['title'],'type'=>$data['type']));
		}

		$game = spClass("m_game");
		if(isset($data['game_new'])&&isset($data['title'])&&isset($data['temple_id'])){
			$game->create(
				array(
					'title'=>$data['title'],
					'password'=>md5($data['key']),
					'temple_id'=>$data['temple_id'],	
				)
			);
		}


		$temple_result = $temple->findAll();
		$this->temple_result = $temple_result;

		$game_result = $game->findAll();
		$this->game_result = $game_result;

		if(!$temple_id&&$temple_result[0]['id']) $temple_id = $temple_result[0]['id'];

		if($temple_id){
			$this->temple_id = $temple_id;

			$point = spClass("m_point");
			$this->point_result = $point->findAll(array('temple_id' => $temple_id));
			$this->center_result = $point->find(array('temple_id' => $temple_id,'type'=>0));
			$this->temple_item = $temple->find(array('id' => $temple_id));

		}
		
	}
	function point_new(){
		$data = $this->spArgs();
		$point = spClass("m_point");

		if(isset($data['title'])&&isset($data['lng'])&&isset($data['lat'])
			&&isset($data['type'])&&isset($data['temple_id'])){
			$point_id = $point->create(
				array('title'=>$data['title'],
					'longitude'=>$data['lng'],
					'latitude'=>$data['lat'],
					'type'=>$data['type'],
					'temple_id'=>$data['temple_id']
				)
			);
			echo $point_id;
		}else{
			echo 0;
		}
		
	}
	function point_delete(){
		$data = $this->spArgs();
		$point = spClass("m_point");

		if(isset($data['point_delete_id'])){
			$point->delete(
				array('id'=>$data['point_delete_id'])
			);
			echo 1;
		}else{
			echo 0;
		}
		
	}
	function game_info(){
		$data = $this->spArgs();
		if($data['game_id']){
			$game = spClass("m_game");
			$point = spClass("m_point");
			$game_point = spClass("m_game_point");

			$game_result = $game->find(array('id' => $data['game_id']));

			$game_point_result = $game_point->findAll(array('game_id' => $data['game_id']));
			foreach($game_point_result as $key=>$item){
				$game_point_result[$item['point_id']] = $item;
			}

			$point_result = $point->findAll(array('temple_id'=>$game_result['temple_id']));
			foreach($point_result as $key=>$item){
				if(isset($game_point_result[$item["id"]])){
					$game_point_item = $game_point_result[$item["id"]];
					$point_result[$key] = $item+array('group_id'=>$game_point_item['group_id'],
						'owner'=>$game_point_item['owner'],'time'=>$game_point_item['time']);
				}
			}
			echo json_encode($point_result);
		}else{
			echo 0;
		}
	}
	function own_point(){
		$data = $this->spArgs();

		if(is_numeric($data['game_id'])&&is_numeric($data['point_id'])
			&&is_numeric($data['group_id'])&&isset($data['owner'])){
			$game_point = spClass("m_game_point");

			$is_point = $game_point->find(array('game_id' => $data['game_id'],'point_id'=>$data['point_id']));
			if(!$is_point){
				$id = $game_point->create(
					array('game_id'=>$data['game_id'],
						'point_id'=>$data['point_id'],
						'group_id'=>$data['group_id'],
						'owner'=>$data['owner']					
					)
				);
				if($id) echo 1;
				else echo 0;
			}else{
				echo 0;
			} 
		}else{
			echo 0;
		}
	}
	function game_point_delete(){
		$data = $this->spArgs();
		if(is_numeric($data['game_id'])){
			$game_point = spClass("m_game_point");
			$game_point->delete(array('game_id'=>$data['game_id']));
			echo 1;
		}else{
			echo 0;
		}
		
	}
}